        TITL DAI FIRMWARE 0E1F5-0E400  V1.1
        ORG   :E1F5
*
*
*
*********************
* RUN basiccmd EDIT *
*********************
*
* The editbuffer is set up by moving the program
* to the end of the free RAM space. All memory
* between heapstart and textbegin is used as
* editbuffer.
* On break + space: The edited area is deleted
* from the textbuffer and the program is moved
* to just after the end of the edited text by
* changing the heapsize.
* EFSW is set for input from editbuffer.
*
REDIT   CALL  REDIN     Init. edit buffer
        CALL  RLS05     List into edit buffer
RED05   XRA   A
        STA   OTSW      Set output to screen
        STA   KNSCAN    Enable complete keyb.scan
        CALL  OTBIN     0 on end of buffer
        LXI   H,:0000
        SHLD  TABTP     Clear tab table pntr
        RST   5         Init screen editor
        DATA  :2A
RED10   CALL  GETC      Get char from keyboard
        JC    RED19     If break pressed
        JZ    RED10     Wait for inputs
        RST   5         Obey character
        DATA  :2D
        JMP   RED10     Get next input
 REM
* If Break pressed:
 REM
RED19   MVI   A,:0C
        RST   5         Clear screen
        DATA  :03
RED20   CALL  GETC      Get char from keyboard
        JC    RED30     If again Break
 REM
* Break followed by any character:
 REM
        JZ    RED20     Wait for a char typed in
        MVI   A,:02
        STA   EFSW      EFSW: input from buffer
        LHLD  LISW2     Get end listed area
        XCHG            in DE
        LHLD  GSNWK     Get start listed area
        CALL  SUBDE     Calc. length listed area
        NOP 
        CALL  PROGM     Delete edited area in txtbuf
        LHLD  HEAP      Get start HEAP
        XCHG            in DE
        LHLD  EBUFN     Get input pntr editbuf
        CALL  SUBDE     Calc length used edit area
        INX   H
        INX   H
        XCHG            DE: length edit area +2
        CALL  HINIT     Program to end of editbuf
        ORA   A         No special conditions
        RET 
 REM
* Break followed by 2nd break:
 REM
RED30   CALL  HRINIT    Restore original Heap +
 REM program buffers
        ORA   A         No special conditions
        RET 
        NOP 
*
******************************
* RUN basiccmd EDIT <linenr> *
******************************
*
REDI1   CALL  REDIN     Init edit buffer
        CALL  RLIS1     List one line
        JMP   RED05     Into Run edit
*
*****************************
* RUN basiccmd EDIT <range> *
*****************************
*
REDI2   CALL  REDIN     Init edit buffer
        CALL  RLIS2     List part of program
        JMP   RED05     Into Run edit
*
****************************
* INITIALISE SCREEN EDITOR *
****************************
*
* Sets up a mode 0 screen, clears all variables
* and arrays. Moves Basic program to top of free
* memory, initialises edit pointers.
*
* Exit: BC preserved. AFDEHL corrupted.
*
REDIN   MVI   A,:FF
        RST   5         Change screen to mode 0
        DATA  :18
        CALL  MPT33     Run 'OUT OF MEMORY' error
 REM if insufficient space. Else
 REM empty HEAP + variables
        CALL  SIZE      Calc free RAM space
        XCHG            in DE
        LHLD  HSIZE     Get HEAPsize
        DAD   D
        XCHG            Total 'free' RAM in DE
        CALL  HINIT     Program to end free RAM
        LHLD  TXTBGN    Get startaddr. textbuf
        DCX   H
        DCX   H         Minus 2
        SHLD  EBUFS     Store end available space
        LHLD  HEAP      Get startaddr HEAP
        INX   H
        INX   H         Plus 2
        SHLD  EBUFR     Store startaddr. editbuf
        SHLD  EBUFN     Set input pntr editbuf
        LXI   H,OTSW
        MVI   M,:02     Set output to editbuf
        RET 
 REM
**************************
* INPUT FROM EDIT BUFFER *
**************************
*
* Entry: A=0, CY=0.
*
IFBNL   PUSH  PSW
        LHLD  EBUFR     Get startaddr. editbuf
        SHLD  EFEPT     Store it in EFEPT
IFB10   MOV   A,M       Get char from editbuf
        ORA   A         Char is 0?
        JZ    IFB20     Then editbuf empty
        INX   H
        CPI   :0D       Car.ret?
        JNZ   IFB10     Get next char if not
 REM
* If char is car.ret:
 REM
        SHLD  EBUFR     Update startaddr. editbuf
        MVI   C,:00     1st pos on line
        POP   PSW       No special action
        RET 
 REM
* If buffer empty:
 REM
IFB20   STA   EFSW      Set input from keyboard
        CALL  HRINIT    Organise HEAP + buffers
        POP   PSW       special action
        STC             CY=1
        RET 
*
**********************
* RUN basiccmd PRINT *
**********************
*
* Entry: BC: Position in textbuffer.
*
RPRINT  LDAX  B         Get length
        INX   B
        MOV   D,A       Count of expr in D
        ORA   A
        JZ    RPR80     Jump if only car.ret
RPR10   PUSH  D         Save nr of expressions
        LDAX  B         Get expr type
        INX   B
        CPI   :20
        JZ    RPR40     Jump if string
 REM
* If INT/FPT number:
 REM
        CPI   :00
        CALL  REXNA     Eval expr. Result in MACC
        PUSH  PSW       Save flags on expr.type
        CNZ   PINT      If INT: print INT number
        POP   PSW
        CZ    PFPT      If FPT: print FPT number
        JMP   RPR50
 REM
* If string:
 REM
RPR40   CALL  REXSR     Evaluate string expr
        PUSH  H
        RST   5         Ask cursor pos and size
        DATA  :0C       char screen
        MOV   A,E       X-size of screen in A
        SUB   L         Minus X-coord cursor pos
        INR   A         +1
        POP   H
        CMP   M         (not used further: off
        NOP             screen printing possible)
        NOP             (should be: CC :DD5E)
        NOP 
        CALL  PSTR      Print string pntd by HL
*
RPR50   LDAX  B         Get byte after string
        INX   B
        CPI   :FF       End marker?
        JZ    RPR70     Then quit with car.ret
        CPI   :3B       ';' ?
        CNZ   PSKP      Cursor to next column if
 REM not (must be ',')
        POP   D         Get nr of expr to print
        DCR   D         Update expr count
        JNZ   RPR10     Loop if more expressions
        ORA   A         No special action
        RET 
 REM
* If end of print statement:
 REM
RPR70   POP   D
RPR80   CALL  CRLF      Print 'CR'
        ORA   A
        RET 
*
**********************
* RUN basiccmd INPUT *
**********************
*
* Runs an input statement with a prompt ('?').
* RINPQ:  Input with a string.
* RINPUT: Input without a string.
*
RINPQ   CALL  REXSR     Evaluate stringexpression
        CALL  PSTR      Print string pntd by HL
RINPUT  LXI   H,:0002
        DAD   SP
        CALL  :E447     Update ERSSF, print '?' and
 REM ask for inputs
        CNC   RRDIP     If no break: store inputs
        PUSH  PSW       Save last input
        XRA   A
        STA   GOSPT     Reset flag running inputs
        POP   PSW       Get last input
        JC    RIP20     Abort if break
        INR   E
        JZ    RIP10     Quit if correct nr of inputs
        CALL  PMSGR     Else: Print
        DBL   MSG01     'SOME INPUT IGNORED'
RIP10   ORA   A         No special action
        RET 
 REM
* If suspended:
 REM
RIP20   MVI   A,:02     Code 'cmd broken in'
        RET 
*
*********************
* RUN basiccmd READ *
*********************
*
RREAD   MVI   A,:01
        STA   EFSW      Set input from string
        LDA   DATCNT    Get offset next char to en-
        MOV   E,A       code and store it in E
        LHLD  DATAQ     Get DATAQ addr
        MOV   A,M       Get length of string
        STA   EFMOD     Store it in EFECT
        INX   H         Pnts to 1st char
        SHLD  EFEPT     Addr 1st char in EFEPT
        CALL  RRDIP     Store data
        MOV   A,E       Get offset next char
        STA   DATCNT    Store it
        LHLD  EFEPT     Get EFEPT
        DCX   H         Pnts to next dataline
        SHLD  DATAQ     Store it in DATAQ
        XRA   A
        STA   EFSW      Set input from keyboard
        RET 
*
*********************
* DATA - (not used) *
*********************
*
L0E351  DATA  :21
        DATA  :01
        DATA  :60
        DATA  :69
        DATA  :22
        DATA  :1F
*
*****************
* RESTART INPUT *
*****************
*
* Error handling if running inputs.
*
INPRS   LHLD  ERSSP     Get saved stackpntr
        SPHL            Reload stackpntr
        LHLD  BRKPT     Get start current command
        MOV   B,H       ) Store it in BC
        MOV   C,L       )
        CALL  PMSGR     Print 'RETYPE LINE'
        DBL   MSG04
        JMP   ST30      Re-execute input statement
*
**********************************
* STORE DATA IN CORRECT LOCATION *
**********************************
*
* Entry L0E56 not used.
*
* Stores data read from datastatements or gotten
* from an input line on the correct location.
*
* Entry: E : Offset next character to encode
*            (#0291).
*        HL: Startaddress data line.
*
L0E56   CALL  MPT36     (not used)
*
RRDIP   LDAX  B         Get nr of data reqd
        INX   B
        MOV   D,A       Into D
RRI10   DCR   D
        JM    RRI90     Jump if ready
RRI20   INR   E         Offset nex char
        JZ    RRI70     Jump if at end of line
        DCR   E
RRI30   CALL  RVAR      Get varptr in HL
        PUSH  B
        PUSH  D
        MOV   C,E       Offset in C
        PUSH  H         Save varptr
        LXI   H,EBUF    Startaddr EBUF
        PUSH  H         Save it on stack
        ANI   :30
        RST   1         Encode a constant
        DATA  :06
        MOV   E,C       Offset in E
        POP   B
        POP   H         Get varptr
        CPI   :20       String type ?
        MOV   A,E       Offset in A
*
****************************
* STORE DATA (READ, INPUT) *
****************************
*
* Handle string inputs (no change):
*
        CZ    RLT80     ] Only JZ :E3A2 replaced
        CNZ   RLT70     ] by its contents
 REM
* Check separator/terminator:
 REM
RRI40   MOV   C,A       Offset in C
        CALL  IGNB      Get char from line, neglect
 REM tab and space
        INR   C         Offset +1
        CPI   :2C       ',' ?
        JZ    RRI50
        CPI   :0D       CR ?
        MVI   C,:FF     Dummy offset for end of
 REM line
RRI50   POP   D
        MOV   E,C       Offset in E
        POP   B
 REM
* Error exit changed: Now checks if end of input
* is reached. EFEPT is only updated if not running
* inputs (anymore).          
 REM
        JZ    RRI10     ] Read next data if no error
        LDA   GOSPT     If error: Get 'run-input'
 REM flag
        CALL  :E3C3     ]
        JMP   ERRSN     ] Run 'SYNTAX ERROR'
 REM
* If end of line reached ('CR'):
 REM
RRI70   LDA   GOSPT     Get flag for running inputs
        ORA   A
        JZ    RRI80     Quit if not running inputs
        CALL  COL0      Cursor to begin next line
        CALL  INPGT     Print '?', read input line
        JC    RRI99     Abort if break pressed
        JMP   RRI30     Cont reading
 REM
* If whole line read:
 REM
RRI80   CALL  DATAF     Find next dataline in txtbuf
        JMP   RRI30     Cont reading
 REM
* If reading done:
 REM
RRI90   ORA   A         No special action
RRI99   RET 
 REM
* If error:
 REM
*
        ANA   A         ] Set flags
        RNZ             ] Abort if running inputs
        LHLD  EFEPT     Get EFEPT
        LXI   D,TICCC
        DAD   D         -4
        SHLD  CURRNT    Set start current line
        RET 
*
**************************
* PREPARE GETTING INPUTS *
**************************
*
* Continuation of 0E447.
* Prints a prompt ('?') on the screen and gets a
* textline from input.
*
INPGT   PUSH  D
        RST   5         Ask cursor pos. and size
        DATA  :0C       char. screen
        POP   D
        MVI   A,:3F
        CALL  PINPLN    Print '?'; input a textline
        MOV   E,L
        INR   E         E pnts after last char of
 REM input
        RET 
*
************************************
* FIND NEXT DATALINE IN TEXTBUFFER *
************************************
*
DATAF   PUSH  PSW
        PUSH  D
        PUSH  H
        LHLD  DATAP     Get addr current dataline
DTF10   MOV   A,M       Get length data string
        ORA   A
        MVI   A,:02
        JZ    ERROR     Run error 'OUT OF DATA' if
 REM no data available
        MOV   D,H       ) Addr dataline in DE
        MOV   E,L       )
        CALL  DADM      Calc end dataline
        SHLD  DATAP     Store it in DATAP
        XCHG            End in DE, start in HL
        INX   H
        INX   H
        INX   H         Pnts to token
        MOV   A,M       Get token
        CPI   :A2       Is it DATA?
        XCHG
        JNZ   DTF10     Check next textline if not
        XCHG
        JMP   MPT36     Continu
*
        DATA  :FF
*
*
*
        UNL
RLS05   EQU   :E19F
OTSW    EQU   :0131
KNSCAN  EQU   :02B9
OTBIN   EQU   :DD75
TABTP   EQU   :00B4
GETC    EQU   :D6BE
EFSW    EQU   :0135
LISW2   EQU   :011B
GSNWK   EQU   :0119
SUBDE   EQU   :DE1A
PROGM   EQU   :C9D1
HEAP    EQU   :029B
EBUFN   EQU   :00A4
HINIT   EQU   :D195
HRINIT  EQU   :DECA
RLIS1   EQU   :E1AA
RLIS2   EQU   :E1B6
MPT33   EQU   :D86D
SIZE    EQU   :EB51
HSIZE   EQU   :029D
TXTBGN  EQU   :029F
EBUFS   EQU   :00A6
EBUFR   EQU   :00A2
EFEPT   EQU   :0132
REXNA   EQU   :E808
PINT    EQU   :DB53
PFPT    EQU   :DB59
REXSR   EQU   :E791
PSTR    EQU   :DB32
PSKP    EQU   :DB0D
CRLF    EQU   :DD5E
GOSPT   EQU   :0117
PMSGR   EQU   :DAFF
MSG01   EQU   :D208
DATCNT  EQU   :0123
DATAQ   EQU   :0291
EFMOD   EQU   :0134
ERSSP   EQU   :011D
BRKPT   EQU   :0102
MSG04   EQU   :DB93
ST30    EQU   :C87F
MPT36   EQU   :E436
RVAR    EQU   :E963
EBUF    EQU   :013E
RLT80   EQU   :E4B8
RLT70   EQU   :E4B4
IGNB    EQU   :DDD2
ERRSN   EQU   :DA0B
COL0    EQU   :DD55
TICCC   EQU   :FFFC
CURRNT  EQU   :0100
PINPLN  EQU   :C6A0
DATAP   EQU   :0124
ERROR   EQU   :D9F5
DADM    EQU   :DE39
        LST
        END 
